package com.zhoug.chart3.formatters;

import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.zhoug.chart3.utils.MPChartUtils;

/**
 * 坐标百分比ValueFormatter
 *
 * @Author 35574
 * @Date 2020/9/14
 * @Description
 */
public class MPPercentAxisLabelFormatter extends ValueFormatter {
    private int decimalPlaces = 2;//小数位数
    private boolean fillEnd=false;//小数位不足是否需要补0

    /**
     * 值是否需要乘以100
     * eg: true 0.25 25% 坐标取值范围:0-1
     * eg: false 25  25%  坐标取值范围:0-100
     */
    private boolean multiply100 = false;

    public MPPercentAxisLabelFormatter() {
    }


    public MPPercentAxisLabelFormatter setMultiply100(boolean multiply100) {
        this.multiply100 = multiply100;
        return this;
    }

    public MPPercentAxisLabelFormatter setFillEnd(boolean fillEnd) {
        this.fillEnd = fillEnd;
        return this;
    }

    /**
     * 设置小数位数
     * @param decimalPlaces
     */
    public MPPercentAxisLabelFormatter setDecimalPlaces(int decimalPlaces) {
        this.decimalPlaces = decimalPlaces;
        return this;
    }

    @Override
    public String getFormattedValue(float value) {
        return MPChartUtils.formatDecimalUp(multiply100 ? value * 100 : value, decimalPlaces, fillEnd) + "%";
    }

    @Override
    public String getAxisLabel(float value, AxisBase axis) {
        if (multiply100) {
            return (int) (value * 100) + "%";
        } else {
            return (int) value + "%";
        }
    }

}
